package com.tutu.controller.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.HyphenateException;
import com.tutu.R;
import com.tutu.constant.RegistRegExConst;
import com.tutu.constant.RegistConst;
import com.tutu.model.Model;

/**
 * @author  图图  2020/10/10
 *
 * 注册页面
 */
public class RegistActivity extends Activity {

    // 用户名
    private EditText et_regist_username;

    // 密码
    private EditText et_regist_userpassword;

    // 密码2
    private EditText et_regist_userpasswordto;

    // 注册按钮
    private Button bt_regist_regist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        // 初始化控件
        initView();
        // 初始化控件监听业务
        initListener();
    }



    private void initView(){
        et_regist_username = (EditText)findViewById(R.id.et_regist_username);
        et_regist_userpassword = (EditText)findViewById(R.id.et_regist_userpassword);
        et_regist_userpasswordto = (EditText)findViewById(R.id.et_regist_userpasswordto);
        bt_regist_regist = (Button) findViewById(R.id.bt_regist_regist);
    }

    private void initListener(){

        // 注册按钮点击事件处理
        bt_regist_regist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                regist();
            }
        });



    }

    /***
     * 注册业务逻辑
     */
    private void regist(){
        // 1.获取用户输入的用户名密码
        String registUserName = et_regist_username.getText().toString();
        String registUserPassword = et_regist_userpassword.getText().toString();
        String registUserPasswordTo = et_regist_userpasswordto.getText().toString();
        // 2.校验用户输入的用户名和密码
        // 2.1 校验用户名部分
        if(TextUtils.isEmpty(registUserName)){
            Toast.makeText(RegistActivity.this, RegistConst.USERNAME_IS_NULL,Toast.LENGTH_SHORT).show();
            return;
        }
        if(!registUserName.matches(RegistRegExConst.REGEXNAME)){
            Toast.makeText(RegistActivity.this, RegistConst.USERNAME_IS_ILLEGAL,Toast.LENGTH_SHORT).show();
            return;
        }

        // 2.2校验密码部分
        if(TextUtils.isEmpty(registUserPassword) || TextUtils.isEmpty(registUserPasswordTo)){
            Toast.makeText(RegistActivity.this, RegistConst.USERPWD_IS_NULL,Toast.LENGTH_SHORT).show();
            return;
        }
        if(!registUserPassword.matches(RegistRegExConst.REGEXPWD)){
            Toast.makeText(RegistActivity.this, RegistConst.USERPWD_IS_ILLEGAL,Toast.LENGTH_SHORT).show();
            return;
        }
        if(!registUserPassword.equals(registUserPasswordTo)){
            Toast.makeText(RegistActivity.this, RegistConst.USERPWD_IS_DIFFERENT,Toast.LENGTH_SHORT).show();
            return;
        }

        // 3.服务器注册账号
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    // 去环信服务器注册账号
                    EMClient.getInstance().createAccount(registUserName,registUserPassword);
                    // 更新UI
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegistActivity.this, RegistConst.SUCCESS,Toast.LENGTH_SHORT).show();
                            openNewPage(RegistActivity.this,LoginActivity.class);
                        }
                    });

                } catch (final HyphenateException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(RegistActivity.this, RegistConst.USER_IS_EXIST_TO_LOGIN,Toast.LENGTH_SHORT).show();
                            openNewPage(RegistActivity.this,LoginActivity.class);
                        }
                    });
                }
            }
        });

    }


    /**
     * 跳转
     * @param packageContext
     * @param cls
     */
    private void  openNewPage(Context packageContext, Class<?> cls){
        // 页面跳转
        Intent intent =  new Intent(packageContext,cls);
        // 启动跳转
        startActivity(intent);
        // 结束当前页
        finish();
    }


}
